using System;
using System.Collections.Generic;

namespace Squamous.Vision
{
	/* Interface for anything that can see the map. 
	 * Use this for units or buildings.
	 */
	interface IObserver
	{
		// The x and y position of this thing on the map.
		int X { get; }
		int Y { get; }
		// How far this thing can see in map cell units.
		int VisionRange { get; }
	}
	
	/* Very basic Fog of War vision checker.
	 * Pass it a list of things that can see and it will tell you if a given 
	 * grid cell is visible.
	 */
	class VisionChecker
	{
		/* Constructor requires knowledge of the map size in order to record
		 * which cells have been seen.
		 */
		public VisionChecker(int mapWidth, int mapHeight)
		{
			m_seenMap = new bool[mapWidth, mapHeight];
		}
	
		// Call this to check if a map cell is currently visible.
		public bool IsVisible(int x, int y, IList<IObserver> observers)
		{
			foreach(IObserver observer in observers)
			{
				int dx = x - observer.X;
				int dy = y - observer.Y;
				/* Comparing the square of distances to avoid an expensive 
				 * square root call.
				 */
				int distSqr = (dx * dx) + (dy * dy);
				int ovr = observer.VisionRange;
				if(distSqr <= (ovr * ovr))
				{
					m_seenMap[x, y] = true;
					return true;
				}
			}
			return false;
		}
		
		// Call this to check if a map cell has ever been seen.
		public bool HasBeenSeen(int x, int y)
		{
			return m_seenMap[x, y];
		}
		
		// Record of seen grid cells.
		// If this 2D array gets too large we can convert it to a bitmap.
		private bool[,] m_seenMap;
	}
}